/**
 *
 * @author pquiring
 *
 * Created : Sept 7, 2013
 *
 */

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javaforce.*;
import javaforce.gl.*;

public class GLProps extends javax.swing.JPanel implements KeyListener {

  /**
   * Creates new form GLProps
   */
  public GLProps(Element e, float _fov) {
    initComponents();
    use3d.setSelected(e.use3d);
    tx.setText(String.format("%.4f", e.tx));
    ty.setText(String.format("%.4f", e.ty));
    tz.setText(String.format("%.4f", e.tz));
    rx.setText(String.format("%.4f", e.rx));
    ry.setText(String.format("%.4f", e.ry));
    rz.setText(String.format("%.4f", e.rz));
    image = new JFImage();
    image.load(getClass().getResourceAsStream("img200.png"));
    previewImage.addKeyListener(this);
    previewImage.setFocusable(true);
    if (e.type == Element.TYPE_CAMERA) {
      use3d.setVisible(false);
      fov.setText(String.format("%.4f", _fov));
    } else {
      fov.setText("60.0");
      fov.setVisible(false);
      fovLabel.setVisible(false);
    }
    width = 588; //previewImage.getWidth();
    height = 377;  //previewImage.getHeight();
    off = new GLOffscreen();
    off.createOffscreen(width, height);
    offimage = new JFImage();
    offimage.setSize(width, height);
    initGL();
    timer = new Timer();
    timer.schedule(new TimerTask() {
      public void run() {
        updateImage();
      }
    },100,100);
    p_up.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        button = buttons.P_UP;
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        button = buttons.NONE;
      }
    });
    p_down.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        button = buttons.P_DOWN;
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        button = buttons.NONE;
      }
    });
    p_left.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        button = buttons.P_LEFT;
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        button = buttons.NONE;
      }
    });
    p_right.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        button = buttons.P_RIGHT;
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        button = buttons.NONE;
      }
    });
    z_up.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        button = buttons.P_IN;
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        button = buttons.NONE;
      }
    });
    z_down.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        button = buttons.P_OUT;
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        button = buttons.NONE;
      }
    });
    r_up.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        button = buttons.R_UP;
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        button = buttons.NONE;
      }
    });
    r_down.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        button = buttons.R_DOWN;
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        button = buttons.NONE;
      }
    });
    r_left.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        button = buttons.R_LEFT;
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        button = buttons.NONE;
      }
    });
    r_right.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        button = buttons.R_RIGHT;
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        button = buttons.NONE;
      }
    });
    rz_up.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        button = buttons.RZ_UP;
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        button = buttons.NONE;
      }
    });
    rz_down.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        button = buttons.RZ_DOWN;
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        button = buttons.NONE;
      }
    });
  }

  /**
   * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
   * content of this method is always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jLabel12 = new javax.swing.JLabel();
    use3d = new javax.swing.JCheckBox();
    preview = new javax.swing.JPanel();
    previewImage = new java.awt.Canvas();
    jPanel2 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    tx = new javax.swing.JTextField();
    ty = new javax.swing.JTextField();
    tz = new javax.swing.JTextField();
    jPanel3 = new javax.swing.JPanel();
    rz = new javax.swing.JTextField();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    ry = new javax.swing.JTextField();
    rx = new javax.swing.JTextField();
    jLabel6 = new javax.swing.JLabel();
    fovLabel = new javax.swing.JLabel();
    fov = new javax.swing.JTextField();
    reset = new javax.swing.JButton();
    jPanel1 = new javax.swing.JPanel();
    p_left = new javax.swing.JButton();
    p_down = new javax.swing.JButton();
    p_right = new javax.swing.JButton();
    p_up = new javax.swing.JButton();
    z_up = new javax.swing.JButton();
    z_down = new javax.swing.JButton();
    jPanel4 = new javax.swing.JPanel();
    r_up = new javax.swing.JButton();
    r_right = new javax.swing.JButton();
    r_left = new javax.swing.JButton();
    r_down = new javax.swing.JButton();
    rz_up = new javax.swing.JButton();
    rz_down = new javax.swing.JButton();

    jLabel12.setText("jLabel12");

    use3d.setText("Enable 3D position");

    preview.setBorder(javax.swing.BorderFactory.createTitledBorder("Preview"));
    preview.setPreferredSize(new java.awt.Dimension(600, 400));

    javax.swing.GroupLayout previewLayout = new javax.swing.GroupLayout(preview);
    preview.setLayout(previewLayout);
    previewLayout.setHorizontalGroup(
      previewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(previewImage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    previewLayout.setVerticalGroup(
      previewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(previewImage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Position"));
    jPanel2.setPreferredSize(new java.awt.Dimension(150, 95));

    jLabel1.setText("X");

    jLabel2.setText("Y");

    jLabel3.setText("Z");

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addComponent(jLabel3)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(tz, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE))
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addComponent(jLabel1)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(tx))
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addComponent(jLabel2)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(ty))
    );
    jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel1)
          .addComponent(tx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel2)
          .addComponent(ty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel3)
          .addComponent(tz, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );

    jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Rotation"));

    jLabel4.setText("Z");

    jLabel5.setText("Y");

    jLabel6.setText("X");

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
    jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(
      jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel3Layout.createSequentialGroup()
        .addComponent(jLabel4)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(rz, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE))
      .addGroup(jPanel3Layout.createSequentialGroup()
        .addComponent(jLabel6)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(rx))
      .addGroup(jPanel3Layout.createSequentialGroup()
        .addComponent(jLabel5)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(ry))
    );
    jPanel3Layout.setVerticalGroup(
      jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel3Layout.createSequentialGroup()
        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel6)
          .addComponent(rx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel5)
          .addComponent(ry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel4)
          .addComponent(rz, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );

    fovLabel.setText("FOV");

    reset.setText("Reset");
    reset.setMargin(new java.awt.Insets(2, 4, 2, 4));
    reset.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        resetActionPerformed(evt);
      }
    });

    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Position"));

    p_left.setIcon(new javax.swing.ImageIcon(getClass().getResource("/left.png"))); // NOI18N
    p_left.setMargin(new java.awt.Insets(0, 0, 0, 0));

    p_down.setIcon(new javax.swing.ImageIcon(getClass().getResource("/down.png"))); // NOI18N
    p_down.setMargin(new java.awt.Insets(0, 0, 0, 0));

    p_right.setIcon(new javax.swing.ImageIcon(getClass().getResource("/right.png"))); // NOI18N
    p_right.setMargin(new java.awt.Insets(0, 0, 0, 0));

    p_up.setIcon(new javax.swing.ImageIcon(getClass().getResource("/up.png"))); // NOI18N
    p_up.setMargin(new java.awt.Insets(0, 0, 0, 0));

    z_up.setIcon(new javax.swing.ImageIcon(getClass().getResource("/up.png"))); // NOI18N
    z_up.setMargin(new java.awt.Insets(0, 0, 0, 0));

    z_down.setIcon(new javax.swing.ImageIcon(getClass().getResource("/down.png"))); // NOI18N
    z_down.setMargin(new java.awt.Insets(0, 0, 0, 0));

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addComponent(p_left)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(p_up)
          .addGroup(jPanel1Layout.createSequentialGroup()
            .addComponent(p_down)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(p_right)))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(z_up)
          .addComponent(z_down)))
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addComponent(p_up)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(p_right)
          .addComponent(p_down)
          .addComponent(p_left)))
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addComponent(z_up)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(z_down))
    );

    jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Rotation"));

    r_up.setIcon(new javax.swing.ImageIcon(getClass().getResource("/up.png"))); // NOI18N
    r_up.setMargin(new java.awt.Insets(0, 0, 0, 0));

    r_right.setIcon(new javax.swing.ImageIcon(getClass().getResource("/right.png"))); // NOI18N
    r_right.setMargin(new java.awt.Insets(0, 0, 0, 0));

    r_left.setIcon(new javax.swing.ImageIcon(getClass().getResource("/left.png"))); // NOI18N
    r_left.setMargin(new java.awt.Insets(0, 0, 0, 0));

    r_down.setIcon(new javax.swing.ImageIcon(getClass().getResource("/down.png"))); // NOI18N
    r_down.setMargin(new java.awt.Insets(0, 0, 0, 0));

    rz_up.setIcon(new javax.swing.ImageIcon(getClass().getResource("/up.png"))); // NOI18N
    rz_up.setMargin(new java.awt.Insets(0, 0, 0, 0));

    rz_down.setIcon(new javax.swing.ImageIcon(getClass().getResource("/down.png"))); // NOI18N
    rz_down.setMargin(new java.awt.Insets(0, 0, 0, 0));

    javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
    jPanel4.setLayout(jPanel4Layout);
    jPanel4Layout.setHorizontalGroup(
      jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel4Layout.createSequentialGroup()
        .addComponent(r_left)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(r_up)
          .addGroup(jPanel4Layout.createSequentialGroup()
            .addComponent(r_down)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(r_right)))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(rz_up)
          .addComponent(rz_down)))
    );
    jPanel4Layout.setVerticalGroup(
      jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
        .addComponent(rz_up)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(rz_down))
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
        .addComponent(r_up)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(r_right)
          .addComponent(r_down)
          .addComponent(r_left)))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addComponent(preview, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addGroup(layout.createSequentialGroup()
            .addComponent(use3d)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(fovLabel)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(fov, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(reset))
          .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(use3d)
          .addComponent(fovLabel)
          .addComponent(fov, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(reset))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(preview, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
  }// </editor-fold>//GEN-END:initComponents

  private void resetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetActionPerformed
    tx.setText("0.0");
    ty.setText("0.0");
    tz.setText("0.0");
    rx.setText("0.0");
    ry.setText("0.0");
    rz.setText("0.0");
    fov.setText("60.0");
  }//GEN-LAST:event_resetActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JTextField fov;
  private javax.swing.JLabel fovLabel;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel12;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabel6;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JPanel jPanel3;
  private javax.swing.JPanel jPanel4;
  private javax.swing.JButton p_down;
  private javax.swing.JButton p_left;
  private javax.swing.JButton p_right;
  private javax.swing.JButton p_up;
  private javax.swing.JPanel preview;
  private java.awt.Canvas previewImage;
  private javax.swing.JButton r_down;
  private javax.swing.JButton r_left;
  private javax.swing.JButton r_right;
  private javax.swing.JButton r_up;
  private javax.swing.JButton reset;
  private javax.swing.JTextField rx;
  private javax.swing.JTextField ry;
  private javax.swing.JTextField rz;
  private javax.swing.JButton rz_down;
  private javax.swing.JButton rz_up;
  private javax.swing.JTextField tx;
  private javax.swing.JTextField ty;
  private javax.swing.JTextField tz;
  private javax.swing.JCheckBox use3d;
  private javax.swing.JButton z_down;
  private javax.swing.JButton z_up;
  // End of variables declaration//GEN-END:variables

  GLScene scene;
  GLModel model;
  GLObject object;
  GLRender render;
  JFImage image;
  java.util.Timer timer;
  GLOffscreen off;
  JFImage offimage;
  int width,height;
  enum buttons {NONE, P_UP, P_DOWN, P_LEFT, P_RIGHT, P_IN, P_OUT, R_UP, R_DOWN, R_LEFT, R_RIGHT, RZ_UP, RZ_DOWN};
  buttons button = buttons.NONE;

  public void save(Element e) {
    if (tx.getText().length() == 0) tx.setText("0");
    if (ty.getText().length() == 0) ty.setText("0");
    if (tz.getText().length() == 0) tz.setText("0");
    if (rx.getText().length() == 0) rx.setText("0");
    if (ry.getText().length() == 0) ry.setText("0");
    if (rz.getText().length() == 0) rz.setText("0");
    if (fov.getText().length() == 0) fov.setText("0");
    e.use3d = use3d.isSelected();
    e.tx = Float.valueOf(tx.getText());
    e.ty = Float.valueOf(ty.getText());
    e.tz = Float.valueOf(tz.getText());
    e.rx = Float.valueOf(rx.getText());
    e.ry = Float.valueOf(ry.getText());
    e.rz = Float.valueOf(rz.getText());
  }

  public float getFOV() {
    return Float.valueOf(fov.getText());
  }

  private void initGL() {
    JFLog.log("GLProps.init()");
    MainPanel.gl.pollEvents();
    scene = new GLScene();
    scene.init(GLVertexShader.source, GLFragmentShader.source);
    render = new GLRender();
    render.init(scene, width, height);
    model = new GLModel();
    scene.addModel(model);
    object = new GLObject();
    model.addObject(object);
    model.addTexture("0");
    //need to calc based on w,h,fov
    float z = 5.0f;
    float x = 2.0f;
    float y = 2.0f;
    GLUVMap map = object.createUVMap();
    map.textureIndex = 0;
    map.texloaded = true;  //direct loaded
    object.addVertex(new float[] {-x,-y,-z}, new float[] {0,1});
    object.addVertex(new float[] {+x,-y,-z}, new float[] {1,1});
    object.addVertex(new float[] {-x,+y,-z}, new float[] {0,0});
    object.addVertex(new float[] {+x,+y,-z}, new float[] {1,0});
    object.addPoly(new int[] {0,3,2});
    object.addPoly(new int[] {0,1,3});
    object.copyBuffers();
    scene.setTexture("0", image.getPixels(), image.getWidth(), image.getHeight(), 0);
    x = Float.valueOf(tx.getText());
    y = Float.valueOf(ty.getText());
    z = Float.valueOf(tz.getText());
    model.translate(x, y, z);
    x = Float.valueOf(rx.getText());
    y = Float.valueOf(ry.getText());
    z = Float.valueOf(rz.getText());
    model.rotate(x, 1.0f, 0, 0);
    model.rotate(y, 0, 1.0f, 0);
    model.rotate(z, 0, 0, 1.0f);
  }

  public void render() {
    try {
      java.awt.EventQueue.invokeAndWait(new Runnable() {
        public void run() {
          MainPanel.gl.pollEvents();
          float x,y,z;
          model.setIdentity();
          x = Float.valueOf(rx.getText());
          y = Float.valueOf(ry.getText());
          z = Float.valueOf(rz.getText());
          model.rotate(x, 1.0f, 0.0f, 0.0f);
          model.rotate(y, 0.0f, 1.0f, 0.0f);
          model.rotate(z, 0.0f, 0.0f, 1.0f);
          x = Float.valueOf(tx.getText());
          y = Float.valueOf(ty.getText());
          z = Float.valueOf(tz.getText());
          model.translate(x,y,z);
          x = Float.valueOf(fov.getText());
          render.fovy = x;
          render.render();
          offimage.putPixels(off.getOffscreenPixels(), 0,0,width,height,0);
          offimage.fillAlpha(0, 0, width, height, 0xff);
          previewImage.getGraphics().drawImage(offimage.getImage(), 0, 0, null);
        }
      });
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void uninit() {
    timer.cancel();
  }

  public void resize(GL gl, int width, int height) {
    render.resize(width, height);
  }

  private static float delta = 0.1f;
  private static float angle = 1.0f;

  public void keyTyped(KeyEvent e) {
  }

  public void keyPressed(KeyEvent e) {
    char ch = e.getKeyChar();
    int cc = e.getKeyCode();
    float f;
    switch (ch) {
      case 'a':
        button = buttons.P_LEFT;
        break;
      case 's':
        button = buttons.P_DOWN;
        break;
      case 'd':
        button = buttons.P_RIGHT;
        break;
      case 'w':
        button = buttons.P_UP;
        break;
      case 'r':
        button = buttons.P_IN;
        break;
      case 'f':
        button = buttons.P_OUT;
        break;
      case 'z':
        button = buttons.RZ_UP;
        break;
      case 'x':
        button = buttons.RZ_DOWN;
        break;
    }
    switch (cc) {
      case KeyEvent.VK_UP:
        button = buttons.R_UP;
        break;
      case KeyEvent.VK_DOWN:
        button = buttons.R_DOWN;
        break;
      case KeyEvent.VK_LEFT:
        button = buttons.R_LEFT;
        break;
      case KeyEvent.VK_RIGHT:
        button = buttons.R_RIGHT;
        break;
    }
  }

  public void keyReleased(KeyEvent e) {
    button = buttons.NONE;
  }

  private void adjustImage(buttons button) {
    float f;
    switch (button) {
      case P_LEFT:
        f = Float.valueOf(tx.getText());
        f -= delta;
        tx.setText(String.format("%.4f", f));
        break;
      case P_DOWN:
        f = Float.valueOf(ty.getText());
        f -= delta;
        ty.setText(String.format("%.4f", f));
        break;
      case P_RIGHT:
        f = Float.valueOf(tx.getText());
        f += delta;
        tx.setText(String.format("%.4f", f));
        break;
      case P_UP:
        f = Float.valueOf(ty.getText());
        f += delta;
        ty.setText(String.format("%.4f", f));
        break;
      case P_IN:
        f = Float.valueOf(tz.getText());
        f += delta;
        tz.setText(String.format("%.4f", f));
        break;
      case P_OUT:
        f = Float.valueOf(tz.getText());
        f -= delta;
        tz.setText(String.format("%.4f", f));
        break;
      case RZ_UP:
        f = Float.valueOf(rz.getText());
        f += angle;
        rz.setText(String.format("%.4f", f));
        break;
      case RZ_DOWN:
        f = Float.valueOf(rz.getText());
        f -= angle;
        rz.setText(String.format("%.4f", f));
        break;
      case R_UP:
        f = Float.valueOf(rx.getText());
        f += angle;
        rx.setText(String.format("%.4f", f));
        break;
      case R_DOWN:
        f = Float.valueOf(rx.getText());
        f -= angle;
        rx.setText(String.format("%.4f", f));
        break;
      case R_LEFT:
        f = Float.valueOf(ry.getText());
        f += angle;
        ry.setText(String.format("%.4f", f));
        break;
      case R_RIGHT:
        f = Float.valueOf(ry.getText());
        f -= angle;
        ry.setText(String.format("%.4f", f));
        break;
    }
  }

  public void updateImage() {
    adjustImage(button);
    render();
  }
}
